package com.lb.jwitter.action.report;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQLDTTXADMKhoa;
import com.lb.jwitter.service.TblQLDTTXADMKhoaService;
import com.lb.jwitter.service.TblQLDTTXADMNganh;
import com.lb.jwitter.service.TblQLDTTXADMNganhService;
import com.lb.jwitter.service.TblQLDTTXADMTrangThaiHocVien;
import com.lb.jwitter.service.TblQLDTTXADMTrangThaiHocVienService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;
import com.lb.jwitter.service.TblQLDTTXALop;
import com.lb.jwitter.service.TblQLDTTXALopService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXATrangThaiHocVienReportAction{
    private Message msg; 
    public Message getMsg() { return msg; }

    private Long trangThaiId;
    private Long khoaId;
    private Long nganhId;
    private Long lopId;
    
   private TblQLDTTXADMKhoaService tblQLDTTXADMKhoaService;
    private TblQLDTTXADMNganhService tblQLDTTXADMNganhService;
    private TblQLDTTXALopService tblQLDTTXALopService;
    private TblQLDTTXADMTrangThaiHocVienService tblQLDTTXADMTrangThaiHocVienService;
    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService;
    
    private List<TblQLDTTXADMKhoa> tblQLDTTXADMKhoas;
    private List<TblQLDTTXADMNganh> tblQLDTTXADMNganhs;
    private List<TblQLDTTXALop> tblQLDTTXALops;
    public Long getTrangThaiId() {
		return trangThaiId;
	}
	public void setTrangThaiId(Long trangThaiId) {
		this.trangThaiId = trangThaiId;
	}
	public List<TblQLDTTXALop> getTblQLDTTXALops() {
		return tblQLDTTXALops;
	}
	public void setTblQLDTTXALops(List<TblQLDTTXALop> tblQLDTTXALops) {
		this.tblQLDTTXALops = tblQLDTTXALops;
	}
	public List<TblQLDTTXADMTrangThaiHocVien> getTblQLDTTXADMTrangThaiHocViens() {
		return tblQLDTTXADMTrangThaiHocViens;
	}
	public void setTblQLDTTXADMTrangThaiHocViens(
			List<TblQLDTTXADMTrangThaiHocVien> tblQLDTTXADMTrangThaiHocViens) {
		this.tblQLDTTXADMTrangThaiHocViens = tblQLDTTXADMTrangThaiHocViens;
	}

	private List<TblQLDTTXADMTrangThaiHocVien> tblQLDTTXADMTrangThaiHocViens;
    
    private String report;
    
    
    
    


  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String form() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien", "xem")){ 
        return "noprivilege"; 
    } 
 
    tblQLDTTXALopService = (TblQLDTTXALopService) context.getBean("tbl_QLDTTXA_LopClient");
        tblQLDTTXADMKhoaService = (TblQLDTTXADMKhoaService) context.getBean("tbl_QLDTTXA_DM_KhoaClient");
        tblQLDTTXADMNganhService = (TblQLDTTXADMNganhService) context.getBean("tbl_QLDTTXA_DM_NganhClient");
        tblQLDTTXADMTrangThaiHocVienService = (TblQLDTTXADMTrangThaiHocVienService) context.getBean("tbl_QLDTTXA_DM_TrangThaiHocVienClient"); 
        
//        tblQLDTTXAHocVienHocPhiMonHocService = (TblQLDTTXAHocVienHocPhiMonHocService) context.getBean("tbl_QLDTTXA_HocVienHocPhi_MonHocClient");
//        tblQLDTTXAHocVienHocPhiService = (TblQLDTTXAHocVienHocPhiService) context.getBean("tbl_QLDTTXA_HocVien_HocPhiClient"); 


        msg = new Message(); 
        List<ConditionReport> cons = new ArrayList<ConditionReport>();
        
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("ten");
            con.setValue1("asc");
            con.setOperator("order");
            cons.add(con);	
        } 
        
        tblQLDTTXALops = tblQLDTTXALopService.findtblQLDTTXALopByCondition(cons, 0, 0);
        tblQLDTTXADMKhoas = tblQLDTTXADMKhoaService.findtblQLDTTXADMKhoaByCondition(cons, 0, 0);
        tblQLDTTXADMNganhs = tblQLDTTXADMNganhService.findtblQLDTTXADMNganhByCondition(cons, 0, 0);
        tblQLDTTXADMTrangThaiHocViens = tblQLDTTXADMTrangThaiHocVienService.findAlltblQLDTTXADMTrangThaiHocVien(0, 0);
        
        
        
        return Action.SUCCESS;
	 }catch(Exception e){ 
	     System.out.println("Loi Report tblQLDTTXATrangThaiHocVien Action"); 
	     e.printStackTrace(); 
	     return Action.ERROR; 
	}
}
 
 
 
 
 public String execute() throws Exception 
 { 
  try{ 	
      Map session = ActionContext.getContext().getSession();
      taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
      if( taiKhoanDangNhap == null) return "noprivilege";
     ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
     tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
     if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien", "xem")){ 
         return "noprivilege"; 
     } 
         tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient");
         
         msg = new Message(); 
         List<ConditionReport> cons = new ArrayList<ConditionReport>();
         
                 
         //lay cac thong tin cua hoc vien
         cons.clear();
         
         
         if(khoaId != null && !khoaId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("khoaId.id");
             con.setValue1(khoaId);
             con.setOperator("eq");
             cons.add(con);	
         } 
         if(nganhId != null && !nganhId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("nganhId.id");
             con.setValue1(nganhId);
             con.setOperator("eq");
             cons.add(con);	
         } 
         if(lopId != null && !lopId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("lopId.id");
             con.setValue1(lopId);
             con.setOperator("eq");
             cons.add(con);	
         } 
         if(trangThaiId != null && !trangThaiId.equals(new Long(0))){ 
             ConditionReport con = new ConditionReport(); 
             con.setCol("trangThaiId.id");
             con.setValue1(trangThaiId);
             con.setOperator("eq");
             cons.add(con);	
         } 
         
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("ten");
             con.setValue1("asc");
             con.setOperator("order");
             cons.add(con);	
         } 
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("hoTen");
             con.setValue1("asc");
             con.setOperator("order");
             cons.add(con);	
         } 

         List<TblQLDTTXAHocVien> tblQLDTTXAHocViens = tblQLDTTXAHocVienService.findtblQLDTTXAHocVienByCondition(cons, 0, 0);
	     		  
		  report ="<tbody>";
		  int stt = 0;
		  DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		  
		  for(TblQLDTTXAHocVien hocVien : tblQLDTTXAHocViens){ 
			   stt++;
		      report += "<tr class='tableitem' > "; 
		      //stt
		      report += "<TD>" +stt+ "</TD>"; 
		      //mshv
		      report += "<td>" + (hocVien.getMSHV() == null ? "" : hocVien.getMSHV()) + "</td> ";
		      //ho ten
		      report += "<td>" + (hocVien.getHoTen() == null ? "" : hocVien.getHoTen())+ "</td> ";
		      report += "<td>" + (hocVien.getTen() == null ? "" : hocVien.getTen())+ "</td> ";
		      
		     report += "<td>" + (hocVien.getLopId() == null ? "" : hocVien.getLopId().getTen()) + "</td> ";
		      report += "<td>" + (hocVien.getKhoaId() == null ? "" : hocVien.getKhoaId().getTen()) + "</td> ";
		      report += "<td>" + (hocVien.getNganhId() == null ? "" : hocVien.getNganhId().getTen()) + "</td> ";
		      report += "<td>" + (hocVien.getTrangThaiId() == null ? "" : hocVien.getTrangThaiId().getTen()) + "</td> ";
		      
		  }

		  report += "</tbody>"; 
  
		  return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Report tblQLDTTXATrangThaiHocVien Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 }

public String getReport() {
	return report;
}

public void setReport(String report) {
	this.report = report;
}

public void setMsg(Message msg) {
	this.msg = msg;
} 

public Long getKhoaId() {
	return khoaId;
}

public void setKhoaId(Long khoaId) {
	this.khoaId = khoaId;
}

public Long getNganhId() {
	return nganhId;
}

public void setNganhId(Long nganhId) {
	this.nganhId = nganhId;
}


public List<TblQLDTTXADMKhoa> getTblQLDTTXADMKhoas() {
	return tblQLDTTXADMKhoas;
}

public List<TblQLDTTXADMNganh> getTblQLDTTXADMNganhs() {
	return tblQLDTTXADMNganhs;
}


 } 
 